package com.waves.core.web.domain.vo;

import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dlw
 * @since 2024-03-19
 */
@Data
@ApiModel("游标翻页返回")
@AllArgsConstructor
@NoArgsConstructor
public class CursorPageBaseVO<T> {

    /**
     *     游标（下次翻页带上这参数）
      */
    private String cursor;

    /**
     *     是否最后一页
      */
    private Boolean isLast = Boolean.FALSE;

    /**
     *     数据列表
      */
    private List<T> list;

    public static <T> CursorPageBaseVO<T> init(CursorPageBaseVO cursorPage, List<T> list) {
        CursorPageBaseVO<T> cursorPageBaseVO = new CursorPageBaseVO<T>();
        cursorPageBaseVO.setIsLast(cursorPage.getIsLast());
        cursorPageBaseVO.setList(list);
        cursorPageBaseVO.setCursor(cursorPage.getCursor());
        return cursorPageBaseVO;
    }

    @JsonIgnore
    public Boolean isEmpty() {
        return CollectionUtil.isEmpty(list);
    }

    public static <T> CursorPageBaseVO<T> empty() {
        CursorPageBaseVO<T> cursorPageBaseVO = new CursorPageBaseVO<T>();
        cursorPageBaseVO.setIsLast(true);
        cursorPageBaseVO.setList(new ArrayList<T>());
        return cursorPageBaseVO;
    }

}
